
library(dplyr)
library(ggplot2)

###LOAD AND PROCESS SECOND EXPERIMENT###

if(length(tryCatch(list.dirs(path="~/Library/CloudStorage/Dropbox/Robots and Trade in Services"),error=function(e) e))==0){
  base.path<-"~/Dropbox/Robots and Trade in Services/"
} else {
  base.path<-"~/Library/CloudStorage/Dropbox/Robots and Trade in Services/"
}


df.raw<-read.csv(paste0(base.path,"Data/Lucid June 1 2022/Robots - Nationalism Follow Up_June 10, 2022_13.36.csv"),
                 header=TRUE, stringsAsFactors = FALSE) %>%
  filter(row_number()>2,Status!="Survey Preview") %>%
  mutate(
    across(
      .cols = c(
        importsgoodpret_1,
        importsgoodpostt_1,
        contains("regs"),
        contains("benefits")
      ),
      ~as.numeric(as.character(.x))
    )
  ) %>% 
  #Factors and level order
  mutate(
    polideology=factor(
      polideology,
      levels=c(
        "Extremely liberal",
        "Liberal",
        "Slightly liberal",
        "Moderate",
        "Slightly conservative",
        "Conservative",
        "Extremely conservative"
      )
    )
  ) %>%
  #Gather outcomes into one column
  mutate(
    regs1=ifelse(is.na(regs1.br_1),regs1.rb_1,regs1.br_1),
    regs2=ifelse(is.na(regs2.br_1),regs2.rb_1,regs2.br_1),
    regs3=ifelse(is.na(regs3.br_1),regs3.rb_1,regs3.br_1),
    benefits1=ifelse(is.na(benefits1.br_1),benefits1.rb_1,benefits1.br_1),
    benefits2=ifelse(is.na(benefits2.br_1),benefits2.rb_1,benefits2.br_1),
    benefits3=ifelse(is.na(benefits3.br_1),benefits3.rb_1,benefits3.br_1)
  ) %>%
  #measure treatment recall
  mutate(recall_correct=case_when(
    treatment==1 & treatmentrecall=="I didn't read any of the above arguments." ~ TRUE,
    treatment==2 & treatmentrecall=="Imported automation technology makes the US too reliant on other countries." ~ TRUE,
    treatment==3 & treatmentrecall=="Imported automation technology helps foreign firms more than US firms.`" ~ TRUE,
    treatment==4 & treatmentrecall=='Imported automation technology hurts blue-collar workers in the "heart" of America.' ~ TRUE,
    TRUE ~ FALSE
  ))

#Limit to those who pass the attention check and took at least 150 seconds
df150<-df.raw %>% 
  filter(
    Q1=="Extremely interested,Very interested",
    Q1.1=="Yes! I will read carefully.",
    Duration..in.seconds.>=150
  )

#Limit to those who pass the attention check, then also took at least 30, 150 seconds
df.raw <-df.raw %>% 
  filter(
    Q1=="Extremely interested,Very interested",
    Q1.1=="Yes! I will read carefully.",
    treatment >=1
  )

df30<-df.raw %>% 
  filter(
    Duration..in.seconds.>=30
  )

df150<-df.raw %>% 
  filter(
    Duration..in.seconds.>=150
  )


###
# Treatment indicators
###

df.raw$tmt.foreign <- ifelse(df.raw$treatment >= 2, 1, 0)

###
# Respondent characteristics
###

df.raw <- df.raw %>% 
  mutate(
    hhi=ifelse(hhi==-3105,NA,hhi),
    education=ifelse(education==-3105,NA,education),
    gender=ifelse(gender=="",NA,gender))

df.raw$male <- ifelse(df.raw$gender == "Male",1,0)
df.raw$hhi.num <- as.numeric(df.raw$hhi)

df.raw$white <- ifelse(df.raw$ethnicity == 1, 1, 0)
df.raw$black <- ifelse(df.raw$ethnicity == 2, 1, 0)
df.raw$aapi <- ifelse(df.raw$ethnicity >= 4 & df.raw$ethnicity <= 14, 1, 0)
df.raw$hispanicbin <- ifelse(df.raw$hispanic != 1 & df.raw$hispanic != 15, 1, 0)

df.raw$educ_nodegree <- ifelse(df.raw$education <= 4, 1, 0)
df.raw$educ_ba <- ifelse(df.raw$education == 5 | df.raw$education == 6, 1, 0)
df.raw$educ_advdegree <- ifelse(df.raw$education == 7 | df.raw$education == 8, 1, 0)


###
# Outcome Measures
###

df.raw$difference1 <- df.raw$regs1-df.raw$benefits1
df.raw$difference2 <- df.raw$regs2-df.raw$benefits2
df.raw$difference3 <- df.raw$regs3-df.raw$benefits3

# Difference (regs minus ben), step 2 for domestic, step 3 for foreign; for between subject comparisons
df.raw$diff.btw23 <- ifelse(df.raw$treatment == 1, df.raw$difference2, df.raw$difference3)

###
# Party ID
###
# Party dummies

df.raw$political_party <- as.numeric(df.raw$political_party)
df.raw$party <- ifelse(df.raw$political_party<=3 | df.raw$political_party==6,"Democrat",
                       ifelse(df.raw$political_party>=8 | df.raw$political_party==5, "Republican",
                              ifelse(df.raw$political_party==4 | df.raw$political_party==7,"Other","-99")))

df.raw$party_dem <- ifelse(df.raw$party=="Democrat",1,0)
df.raw$party_rep <- ifelse(df.raw$party=="Republican",1,0)
df.raw$party_other <- ifelse(df.raw$party=="Other",1,0)


